package com.example.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.common.core.domain.R;
import com.example.common.core.enums.ResultCode;
import com.example.common.core.enums.UserIdentity;
import com.example.common.security.exception.ServiceException;
import com.example.system.domain.chat.GroupMember;
import com.example.system.domain.course.Course;
import com.example.system.domain.course.dto.CourseQueryDTO;
import com.example.system.domain.course.dto.CourseSaveDTO;
import com.example.system.domain.course.dto.CourseStatusUpdateDTO;
import com.example.system.domain.course.vo.CourseDetailVO;
import com.example.system.domain.course.vo.CourseVO;
import com.example.system.mapper.course.CourseMapper;
import com.example.system.service.ICourseService;
import com.example.system.service.IGroupService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class CourseServiceImpl implements ICourseService {

    @Autowired
    private IGroupService groupService;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(CourseSaveDTO courseAddDTO) {

        Course course = new Course();
        BeanUtil.copyProperties(courseAddDTO, course);
        course.setVideoNum(0);
        if(courseAddDTO.getClassroomId()!=null){
            GroupMember groupMember = new GroupMember();
            groupMember.setGroupId(courseAddDTO.getClassroomId());
            groupMember.setUserId(courseAddDTO.getTeacherId());
            groupMember.setNickname(courseAddDTO.getTeacherName());
            groupMember.setIdentity(UserIdentity.TEACHER.getValue());
            groupService.addGroupMember(groupMember);
        }
        return courseMapper.insert(course);
    }

    @Override
    public List<CourseVO> list(CourseQueryDTO courseQueryDTO) {
        PageHelper.startPage(courseQueryDTO.getPageNum(), courseQueryDTO.getPageSize());
        List<CourseVO> courseVOS = courseMapper.selectCourseList(courseQueryDTO);
        return courseVOS;
    }

    @Override
    public CourseDetailVO detail(Long courseId) {
        // 判断资源是否存在
        Course course = courseMapper.selectById(courseId);
        if (course == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        CourseDetailVO courseDetailVO = new CourseDetailVO();
        BeanUtil.copyProperties(course, courseDetailVO);
        return courseDetailVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(CourseSaveDTO courseSaveDTO) {
        Course course = courseMapper.selectById(courseSaveDTO.getCourseId());
        if (course == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        course.setCourseName(courseSaveDTO.getCourseName());
        course.setTeacherId(courseSaveDTO.getTeacherId());
        course.setClassroomId(courseSaveDTO.getClassroomId());
        course.setTeacherName(courseSaveDTO.getTeacherName());
        // 暂时不考虑缓存
        return courseMapper.updateById(course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long courseId) {
        Course course = courseMapper.selectById(courseId);
        if (course == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        // 暂时不考虑缓存

        if(course.getClassroomId()!=null){
            GroupMember groupMember = new GroupMember();
            groupMember.setGroupId(course.getClassroomId());
            groupMember.setUserId(course.getTeacherId());
            groupService.delGroupMember(groupMember);
        }
        return courseMapper.deleteById(courseId);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<String> reviewCourse(CourseStatusUpdateDTO courseStatusUpdateDTO) {
        // 查询课程
        Course course = courseMapper.selectById(courseStatusUpdateDTO.getCourseId());
        if (course == null) {
            return R.fail(ResultCode.FAILED_NOT_EXISTS);
        }
        
        Integer targetStatus = courseStatusUpdateDTO.getStatus();
        
        // 验证状态流转的合法性
        switch (targetStatus) {
            case Course.STATUS_PUBLISHED:
                // 只有待审核的课程才能被发布
                if (course.getStatus() != Course.STATUS_REVIEWING) {
                    return R.fail(ResultCode.FAILED);
                }
                break;
            case Course.STATUS_REJECTED:
                // 只有待审核的课程才能被驳回
                if (course.getStatus() != Course.STATUS_REVIEWING) {
                    return R.fail(ResultCode.FAILED);
                }
                break;
            default:
                return R.fail(ResultCode.FAILED);
        }
        
        // 更新课程状态
        course.setStatus(targetStatus);
        courseMapper.updateById(course);
        
        return R.ok("操作成功");
    }
    
    @Override
    public List<CourseVO> getPendingReviewCourses(CourseQueryDTO courseQueryDTO) {
        PageHelper.startPage(courseQueryDTO.getPageNum(), courseQueryDTO.getPageSize());
        
        // 设置查询条件为待审核状态
        courseQueryDTO.setStatus(Course.STATUS_REVIEWING);
        
        return courseMapper.selectCourseList(courseQueryDTO);
    }
}
